package last.project;

import java.util.ArrayList;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class GameOver extends Activity {
	private Context cxt;
	
	private boolean newHighScore, win;
	private ArrayList<String> Names;
	private ArrayList<Integer> Scores;
	private int Score;
	private static int MAX_X, MAX_Y;
	private static int BUTTON_TEXT_SIZE=FinalProjectActivity.DEFAULT_BUTTON_TEXT_SIZE;
	
	private EditText NameField;
	private Button Done, Continue, NoSubmit;
	@SuppressWarnings("unchecked")
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        cxt=this;
        setContentView(R.layout.gameoverlayout);
        
        Bundle backpack = getIntent().getExtras();
        newHighScore = (Boolean)backpack.get("newHighScore");
        win = (Boolean)backpack.get("win");
        Score = (Integer) backpack.get("Score");
        Names = FinalProjectActivity.HSSystem.Names;
        Scores = FinalProjectActivity.HSSystem.Scores;
        System.out.println(Scores.get(0));
        
        MAX_X=FinalProjectActivity.MAX_X;
        MAX_Y=FinalProjectActivity.MAX_Y;
        
        makeLayout();
        setupButtons();
	}
	private void makeLayout() {
		AbsoluteLayout layout = (AbsoluteLayout) findViewById(R.id.AbsoluteLayout1);
        AbsoluteLayout.LayoutParams params;
        
        /*params = new AbsoluteLayout.LayoutParams(BUTTON_WIDTH*2-MARGIN, BUTTON_HEIGHT-MARGIN, 0, RESULT_FIELD_HEIGHT+4*BUTTON_HEIGHT);
		NumberButtons[0]=new Button(this);
		NumberButtons[0].setText("0");
		layout.addView(NumberButtons[0], params);*/
        
        //win/lose text
        params = new AbsoluteLayout.LayoutParams(320, 80, MAX_X/2-160, MAX_Y/5);
        TextView winLose = new TextView(this);
        if (win)
        	winLose.setText("You win!");
        else
        	winLose.setText("You lose!!");
        winLose.setTextSize(48);
        layout.addView(winLose, params);
        
        params = new AbsoluteLayout.LayoutParams(320, 65, MAX_X/2-160, 3*MAX_Y/4);
        Continue = new Button(cxt);
        Continue.setText("Continue");
        Continue.setTextSize(BUTTON_TEXT_SIZE);
        layout.addView(Continue, params);
        
        if (newHighScore) {
        	Continue.setText("Submit");
        	
        	params = new AbsoluteLayout.LayoutParams(240, 60, MAX_X/2-120, MAX_Y/5+130);
        	TextView congrats = new TextView(this);
        	congrats.setTextSize(12);
        	int place=1;
        	for (int i=0; i<Scores.size() && Scores.get(i)>=Score; i++) {
        		place++;
        	}
        	String placement = OrdinalShorthandNumbers(place);
        	congrats.setText("Congratulations! You got the "+placement+" all time score!");
        	layout.addView(congrats, params);
        	
        	params = new AbsoluteLayout.LayoutParams(220, 70, MAX_X/2-160, MAX_Y/5+180);
        	NameField = new EditText(cxt);
        	NameField.setHint("Name");
        	layout.addView(NameField, params);
        	
        	params = new AbsoluteLayout.LayoutParams(90, 70, MAX_X/2+70, MAX_Y/5+180);
        	Done = new Button(cxt);
        	Done.setText("Done");
        	Done.setTextSize(BUTTON_TEXT_SIZE);
        	layout.addView(Done, params);
        	
        	params = new AbsoluteLayout.LayoutParams(320, 65, MAX_X/2-160, 3*MAX_Y/4+70);
        	NoSubmit = new Button(cxt);
        	NoSubmit.setText("Continue");
        	NoSubmit.setTextSize(BUTTON_TEXT_SIZE);
        	layout.addView(NoSubmit, params);
        	
        } else {
        	params = new AbsoluteLayout.LayoutParams(240, 60, MAX_X/2-120, MAX_Y/5+100);
        	TextView neededScore = new TextView(this);
        	neededScore.setTextSize(12);
        	if (Scores.size()>0)
        		neededScore.setText("You needed at least "+(Scores.get(Scores.size()-1)+1)+" to be in the High Scores");
        	else {
        		neededScore.setText("Sorry, no High Scores at this point");
        		neededScore.setTextSize(10);
        	}
        	layout.addView(neededScore, params);
        }
        
        //add buttons to replay or get out of that screen
	}
	private void setupButtons() {
		if (Done!=null) {
			Done.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					InputMethodManager imm = (InputMethodManager)getSystemService(
						      Context.INPUT_METHOD_SERVICE);
						imm.hideSoftInputFromWindow(NameField.getWindowToken(), 0);
				}
			});
		}
		Continue.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				boolean nextScreen=true;
				if (newHighScore) {
					if (blankString(NameField.getText().toString()) || NameField.getText().toString().equals("Name")) {
						Toast.makeText(cxt, (CharSequence)"Please enter a valid name", Toast.LENGTH_SHORT).show();
						nextScreen=false;
					} else {
						FinalProjectActivity.HSSystem.addHighScore(NameField.getText().toString(), Score);
					}
				}
				if (nextScreen) {
					Intent tent = new Intent(cxt, HighScoreScreen.class);
					cxt.startActivity(tent);
				}
			}
		});
		final DialogInterface.OnClickListener ConfirmedNoSubmit = new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface arg0, int arg1) {
				Intent tent = new Intent(cxt, HighScoreScreen.class);
				cxt.startActivity(tent);
			}
		};
		if (NoSubmit!=null) {
			NoSubmit.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					AlertDialog.Builder bob = new AlertDialog.Builder(cxt).setTitle((CharSequence) ("Confirm"));
					bob.setMessage((CharSequence) "You don't want to save your score? You won't get another chance to.");
					bob.setNegativeButton((CharSequence) "Cancel", new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface arg0, int arg1) {
						}
					});
					bob.setPositiveButton((CharSequence) "Don't Submit", ConfirmedNoSubmit);
					bob.show();
				}
			});
		}
	}
	private boolean blankString(String str) {
		while (str.length()>0) {
			if (!str.substring(0,1).equals(" ")) {
				return false;
			}
			str=str.substring(1);
		}
		return true;
	}
	private String OrdinalShorthandNumbers(int n) {
		int tn=n%10;
		if (tn==1)
			return ""+n+"st";
		if (tn==2)
			return ""+n+"nd";
		if (tn==3)
			return ""+n+"rd";
		else
			return ""+n+"th";
	}
}
